package com.example.threadtest.service.impl;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;

import java.util.concurrent.Future;

/**
 * @author 越
 * @createDate 2021-04-06 16:18
 * @desc 简单实现有关异步和同步两种计算方式的性能比较
 */
/**
 * 操作计算 service 类：
 */
@Service
public class AsyncServiceImpl {

    private final static Logger logger = LoggerFactory.getLogger(AsyncServiceImpl.class);

    public static final int DoTime = 5000;

    /**
     * 异步任务 只需要在所需实现异步的方法上加上@Async注解， 并通过Future<T>来接受异步方法的处理结果
     * 通过@Async注解表明该方法是个异步方法，如果注解在类级别，则表明该类所有的方法都是异步方法
     *
     * @return
     */
    @Async
    public Future<Long> subByAsync() throws Exception {
        long start = System.currentTimeMillis();
        long sum = 0;
        Thread.sleep(3000);
        long end = System.currentTimeMillis();
        sum = end - start;
        logger.info(Thread.currentThread().getName()+"注解任务一subByAsync执行的时间是： " + sum + "（毫秒）");
        return new AsyncResult<>(sum);
    }
    @Async
    public Future<Long> subByAsync2() throws Exception {
        long start = System.currentTimeMillis();
        long sum = 0;
        Thread.sleep(5000);
        long end = System.currentTimeMillis();
        sum = end - start;
        logger.info(Thread.currentThread().getName()+"注解任务一subByAsync2执行的时间是： " + sum + "（毫秒）");
        return new AsyncResult<>(sum);
    }

    /**
     * 仅使用异步注解的方式实现异步方法
     *
     * @return
     */
    @Async
    public void subByVoid() throws Exception {
        long start = System.currentTimeMillis();
        long sum = 0;
        Thread.sleep(DoTime);
        long end = System.currentTimeMillis();
        sum = end - start;
        logger.info(Thread.currentThread().getName()+"注解任务二执行的时间是： " + sum + "（毫秒）");
    }

    /**
     * 使用同步计算的方式--同步调用
     *
     * @return
     */
    public long subBySync() throws Exception {
        long start = System.currentTimeMillis();
        long sum = 0;
        Thread.sleep(3000);
        long end = System.currentTimeMillis();
        sum = end - start;
        logger.info(Thread.currentThread().getName()+"注解任务三subBySync执行的时间是： " + sum + "（毫秒）");
        return sum;
    }
    /**
     * 使用同步计算的方式--同步调用
     *
     * @return
     */
    public long subBySync2() throws Exception {
        long start = System.currentTimeMillis();
        long sum = 0;
        Thread.sleep(5000);
        long end = System.currentTimeMillis();
        sum = end - start;
        logger.info(Thread.currentThread().getName()+"注解任务三subBySync2执行的时间是： " + sum + "（毫秒）");
        return sum;
    }

    @Async
    public Future<Integer> doMineAsync(int i) throws Exception {
        System.out.println(Thread.currentThread().getName()+"------\t:" + i);
        Thread.sleep(5000);
        return new AsyncResult<>(i);
    }
    public int doMineAsync2(int i) throws Exception {
        System.out.println(Thread.currentThread().getName()+"------\t:" + i);
        Thread.sleep(5000);
        return i;
    }
}
